import { getGoodsList,orderSave } from '@/services/api';

const OrderModel = {
  namespace: 'order',
  state: {
    goodsList:[],
    orderId:null
  },
  effects: {
    *getGoodsList({payload}, { call, put }) {
      const response = yield call(getGoodsList,payload);
      yield put({
        type: 'goodsList',
        payload: response?response.result:[],
      });
    },
    *orderSave({payload}, { call, put }) {
      const response = yield call(orderSave,payload);
      console.log(response);
      yield put({
        type: 'orderId',
        payload: response?response.result:null,
      });
    },
  },
  reducers: {
    goodsList(state, action) {
      let data = [];
      if(action.payload && action.payload.list){
        data = action.payload.list;
      }
      console.log("商品",data);
      return {
        ...state,
        goodsList: data,
      };
    },
    orderId(state, action) {
      let data = null;
      if(action.payload && action.payload.id){
        data = action.payload.id;
      }
      console.log("id",data);
      return {
        ...state,
        orderId: data,
      };
    },
  },
};
export default OrderModel;
